package com.andnnl.fileanalysis;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 字符串列表直接传List<String>，目前固定为逗号分隔符(暂时废用)
 * Created by chenss on 2021/6/2.
 */
public class StringListAnalysis {
    private static final Logger logger = LoggerFactory.getLogger(StringListAnalysis.class);
    private List<List<String>> list = new ArrayList<>();

    public StringListAnalysis() {

    }

    public void analysis(List<String> list, AnalysisListenerBase listener, AnalysisParam param) {
        try {
            int rowCount = 0;
            for (String line : list) {
                if (StrUtil.isNotBlank(line) && (param.getMaxRow() == 0 || rowCount < param.getMaxRow())) {
                    line = line.trim();
                    if(param.getSplitLimit()==-1){
                        if (param.getDelHeadRows() > param.getHeadRowCount()) {
                            param.getHeadRowCountAndAdd();
                            logger.info("ignore head:" + line);
                            handleHead(Arrays.asList(line), listener, param);
                            continue;
                        }
                        rowCount++;
                        handleRow(Arrays.asList(line), listener, param);//处理第一行
                    }else {
                        String[] strArr = line.split(param.getSplitRegex(), param.getSplitLimit());
                        if (strArr != null && strArr.length > 0) {
                            if (param.getDelHeadRows() > param.getHeadRowCount()) {
                                param.getHeadRowCountAndAdd();
                                logger.info("ignore head:" + Arrays.toString(strArr));
                                handleHead(Arrays.asList(strArr), listener, param);//处理第一行
                                continue;
                            }
                            rowCount++;
                            handleRow(Arrays.asList(strArr), listener, param);//处理第一行
                        }
                    }

                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            finish(listener, param);//文件读取完成
            listener.finish();//监听完成
        }

    }

    private void handleHead(List<String> row, AnalysisListenerBase listener, AnalysisParam param) {
        listener.invokeHead(row);
    }

    public void handleRow(List<String> row, AnalysisListenerBase listener, AnalysisParam param) {
        if (param.getBatchSize() > 1) {
            list.add(row);
            if (list.size() == param.getBatchSize()) {
                listener.invokeBatch(list);
                list.clear();
            }
        } else {
            listener.invoke(row);
        }
    }

    /**
     * 读取完成之后调用该方法，如果是最后一批
     *
     * @param listener
     * @param param
     */
    public void finish(AnalysisListenerBase listener, AnalysisParam param) {
        if (param.getBatchSize() > 1 && list.size() > 0) {
            listener.invokeBatch(list);
            list.clear();
        }
    }

}
